package com.tedu.egmvcboot.interceptor;

import com.tedu.egmvcboot.pojo.vo.UserVO;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 用于验证用户是否登录的拦截器
 * */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        HttpSession session = request.getSession();
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO==null){//没有登陆
            response.sendRedirect("/index.html");
            return false;//拦截
        }else {
            return true;//放行
        }

    }
}
